/**
 * @(#)HttpXmlClientHandler.java, 2018/12/27
 * <p/>
 * Copyright 2018 Netease, Inc. All rights reserved.
 * NETEASE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.chengzhi.netty.http.xml;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author 阮承志(wb.ruanchengzhi @ mesg.corp.netease.com)
 **/
public class HttpXmlClientHandler extends SimpleChannelInboundHandler<HttpXmlResponse> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(create(123));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpXmlResponse msg) throws Exception {
        System.out.println("the client receive message headers:" + msg.getHttpResponse().headers().names());
        System.out.println("the client receive message body:" + msg.getResult());
    }

    private Order create(long orderId) {
        Order order = new Order();
        order.setOrderNumber(orderId);
        order.setTotal(9999.99f);
        Address address = new Address();
        address.setCity("杭州市");
        address.setCountry("中国");
        address.setPostCode("12311323");
        address.setState("浙江省");
        address.setStreet1("网商路");
        address.setStreet2("999");
        order.setBillto(address);
        Customer customer = new Customer();
        customer.setCustomerNumber(orderId);
        customer.setFirstName("x");
        customer.setLastName("xx");
        order.setCustomer(customer);
        order.setShipping(Shipping.INTERNATIONAL_MAIL);
        order.setShipTo(address);
        return order;
    }

}
